package org.wannatrak.android.activity;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import org.wannatrak.android.ApplicationContext;
import org.wannatrak.android.R;
import org.wannatrak.android.helper.UIHelper;
import org.wannatrak.android.intf.DialogAction;
import org.wannatrak.android.maps.PointsOverlay;

import java.util.List;

/**
 * @Author: Maksim Tulupov maxtulupov@gmail.com
 * @Date: $Date: 2011-06-23 11:04:11 +0000 (Thu, 23 Jun 2011) $ 2:11 PM
 * @version: $Revision: 18 $
 * @id: $Id: MapViewDialogHandler.java 18 2011-06-23 11:04:11Z maxtulupov@gmail.com $
 */
public class MapViewDialogHandler extends Handler {

    private MapActivity mapActivity;

    public MapViewDialogHandler(MapActivity mapActivity) {
        this.mapActivity = mapActivity;
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what >= 0) {
            List<OverlayItem> overlays = (List<OverlayItem>) msg.obj;
            MapView mapView = (MapView) mapActivity.findViewById(R.id.mapView);
            mapView.getOverlays().clear();
            List<Overlay> mapOverlays = mapView.getOverlays();
            Drawable drawable = mapActivity.getResources().getDrawable(android.R.drawable.star_big_on);
            PointsOverlay itemizedoverlay = new PointsOverlay(drawable, mapView);
            itemizedoverlay.addOverlays(overlays);
            mapOverlays.add(itemizedoverlay);
            if (!overlays.isEmpty()) {
                OverlayItem item = overlays.get(overlays.size() / 2);
                mapView.getController().setCenter(item.getPoint());
            }
        } else {
            UIHelper.createAlertDialog(mapActivity, (String) msg.obj, -1, true, (DialogAction[]) null).show();
        }
    }
}
